<?php

include_once("../config.inc");
include_once(ROOT.'/database/config.php');

function manage_open_login($type, $token_1st, $token_2nd, $token_3rd, $nickname, $head_url)
{
	$connect_id = mysql_connect(DB_URL, DB_USERNAME, DB_PASSWORD);
	if ($connect_id)
  {
  	mysql_select_db(DB_DBNAME, $connect_id);
  	
  	$sql = mysql_query("SELECT * FROM manage_open_user WHERE " 
  		. "token_1st='" . $token_1st 
  		. "' AND token_2nd='" . $token_2nd 
  		. "' AND token_3rd='" . $token_3rd 
  		. "' AND type=" . $type);
  	if (0 == mysql_num_rows($sql))
  	{
  		//$data = mysql_fetch_array($sql);
  		mysql_query("INSERT INTO manage_open_user (token_1st, token_2nd, token_3rd, nickname, head_url, local_user_id, type) VALUES ('" 
  			. $token_1st . "', '" . $token_2nd . "', '" . $token_3rd . "', '" . $nickname . "', '" . $head_url . "', " 
  			. 1 . ", " . $type.")");
  	}
  	else
  	{
  	}
  	
  	mysql_close($connect_id);
  }
}

function manage_get_open_login($type)
{
	$connect_id = mysql_connect(DB_URL, DB_USERNAME, DB_PASSWORD);
	if ($connect_id)
  {
  	mysql_select_db(DB_DBNAME, $connect_id);
  	
  	$result = mysql_query("SELECT * FROM manage_open_user WHERE type=" . $type);
  	if (0 == mysql_num_rows($result))
  	{
  	}
  	else
  	{
  		while ($row = mysql_fetch_array($result))
  		{
  			$name = $row['nickname'];
  		}
  	}
  	
  	mysql_close($connect_id);
  }
  
  if(!isset($name))
  	$name = "";
  
  return $name;
}

function manage_get_all_open_login()
{
	$all = array('sina', 'tencent', 'sohu', 'netease');
	
	$connect_id = mysql_connect(DB_URL, DB_USERNAME, DB_PASSWORD);
	if ($connect_id)
  {
  	mysql_select_db(DB_DBNAME, $connect_id);
  	
  	$result = mysql_query("SELECT * FROM manage_open_user");
  	if (0 == mysql_num_rows($result))
  	{
  	}
  	else
  	{
  		while ($row = mysql_fetch_array($result))
  		{
  			$one = array('type', 'nickname', 'token_1st', 'token_2nd', 'token_3rd');
  			$one['type'] = $row['type'];
  			$one['nickname'] = $row['nickname'];
  			$one['token_1st'] = $row['token_1st'];
  			$one['token_2nd'] = $row['token_2nd'];
  			$one['token_3rd'] = $row['token_3rd'];
  			
  			switch ($one['type'])
  			{
  				case 1:
  				{
  					$all['sina'] = $one;
  				}
  				break;
  				case 2:
  				{
  					$all['tencent'] = $one;
  				}
  				break;
  				case 3:
  				{
  					$all['sohu'] = $one;
  				}
  				break;
  				case 4:
  				{
  					$all['netease'] = $one;
  				}
  				break;
  			}
  		}
  	}
  	
  	mysql_close($connect_id);
  }
  
  return $all;
}
?>
